    package com.atguigu.ssyx.acl.controller;


    import com.atguigu.ssyx.acl.service.PerMissionService;
    import com.atguigu.ssyx.common.result.Result;
    import com.atguigu.ssyx.model.acl.Role;
    import com.atguigu.ssyx.acl.service.RoleService;
    import com.atguigu.ssyx.vo.acl.RoleQueryVo;
    import com.baomidou.mybatisplus.core.metadata.IPage;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.bind.annotation.*;

    import javax.annotation.Resource;
    import java.util.List;
    import java.util.Map;

    /**
     * 角色管理
     */
    @RestController
    @RequestMapping("/admin/acl")

    @Api(tags = "用户管理")
    @Slf4j
//    @CrossOrigin
    public class RoleController {

        //注入Service调用接口方法
        @Resource
        private RoleService roleService;
        @Resource
        private PerMissionService permissionService;


        //为角色进行分配菜单
        @ApiOperation("为角色进行分配菜单")
        @PostMapping("/permission/doAssign")
        public Result doAssign(@RequestParam Long roleId,
                               @RequestParam Long[] permissionId){
            permissionService.saveRolePermission(roleId,permissionId);
            return Result.ok(null);
        }




        //获取所有角色，和根据用户id查询用户分配角色列表
//        @ApiOperation(value = "获取所有角色")
//        @GetMapping("/toAssign/{adminId}")
//        public  Result toAssign(@PathVariable Long adminId){
//            //返回map集合包含两部分数据： 所有角色 和 为用户分配角色列表
//            Map<String,Object> map = roleService.getRoleByAdminId(adminId);
//            return Result.ok(map);
//        }


        //获取所有菜单，和根据角色id查询角色分配菜单列表
        @ApiOperation("获取所有菜单")
        @GetMapping("/permission/toAssign/{roleId}")
        public  Result toAssign(@PathVariable Long roleId){
            //返回map集合包含两部分数据： 所有角色 和 为用户分配角色列表
            Map<String,Object> map =permissionService.getPermissionRoleId(roleId);
            return Result.ok(map);
        }





        //1 角色列表（条件分页查询）
        @ApiOperation(value = "角色列表（条件分页查询）")
        @GetMapping("/role/{current}/{limit}")
        public Result pageList(@PathVariable Long current,
                                @PathVariable Long limit,
                                RoleQueryVo roleQueryVo){

            //1 创建page对象， 传递当前页和每页记录数
            //current:当前页
            //limit :每页显示记录数
            Page<Role> pageParam = new Page<>(current,limit);


            //2 调用Service方法实现条件分页查询，返回分页对象
            IPage<Role>pageModel = roleService.selectPage(pageParam,roleQueryVo);

            return Result.ok(pageModel);

        }

        @ApiOperation(value = "获取角色")
        @GetMapping("/role/get/{id}")
        public Result get(@PathVariable Long id) {
            Role role = roleService.getById(id);
            return Result.ok(role);
        }

        @ApiOperation(value = "新增角色")
        @PostMapping("/role/save")
        public Result save(@RequestBody Role role) {
            roleService.save(role);
            return Result.ok(null);
        }

        @ApiOperation(value = "修改角色")
        @PutMapping("/role/update")
        public Result updateById(@RequestBody Role role) {
            roleService.updateById(role);
            return Result.ok(null);
        }

        @ApiOperation(value = "删除角色")
        @DeleteMapping("/role/remove/{id}")
        public Result remove(@PathVariable Long id) {
            roleService.removeById(id);
            return Result.ok(null);
        }

        @ApiOperation(value = "根据id列表删除角色")
        @DeleteMapping("/role/batchRemove")
        public Result batchRemove(@RequestBody List<Long> idList) {
            roleService.removeByIds(idList);
            return Result.ok(null);
        }

    }
